package com.techzhi.address_server.autoconfigure;

import com.techzhi.addres_score.mapper.CityMapper;
import com.techzhi.addres_score.mapper.DistrictMapper;
import com.techzhi.addres_score.mapper.ProvinceMapper;
import com.techzhi.addres_score.mapper.UserAddressMapper;
import com.techzhi.addres_score.service.AddressCacheService;
import com.techzhi.addres_score.service.AddressService;
import com.techzhi.addres_score.service.impl.AddressCacheServiceImpl;
import com.techzhi.addres_score.service.impl.AddressServiceImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@ConditionalOnClass(AddressService.class)
@EnableConfigurationProperties(AddressProperties.class)
public class AddressAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ProvinceMapper provinceMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return sqlSessionFactory.getConfiguration().getMapper(ProvinceMapper.class, (SqlSession) sqlSessionFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public CityMapper cityMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return sqlSessionFactory.getConfiguration().getMapper(CityMapper.class, (SqlSession) sqlSessionFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public DistrictMapper districtMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return sqlSessionFactory.getConfiguration().getMapper(DistrictMapper.class, (SqlSession) sqlSessionFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public UserAddressMapper userAddressMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
        return sqlSessionFactory.getConfiguration().getMapper(UserAddressMapper.class, (SqlSession) sqlSessionFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public AddressService addressService(ProvinceMapper provinceMapper,
                                         CityMapper cityMapper,
                                         DistrictMapper districtMapper,
                                         UserAddressMapper userAddressMapper,
                                         RedisTemplate<String, Object> redisTemplate) {
        return new AddressServiceImpl(provinceMapper, cityMapper, districtMapper,
                userAddressMapper, redisTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public AddressCacheService addressCacheService(ProvinceMapper provinceMapper,
                                                   CityMapper cityMapper,
                                                   DistrictMapper districtMapper,
                                                   RedisTemplate<String, Object> redisTemplate) {
        return new AddressCacheServiceImpl(provinceMapper, cityMapper, districtMapper, redisTemplate);
    }
}
